<template >
    <div v-title data-title="码神之路" style="width:65%;max-width: 80%">

        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="文章" label-width="100">
            <!-- <el-input v-model="formInline.articleId" placeholder="标题"></el-input> -->
            <el-autocomplete
                  v-model="formInline.articleId"
                  :fetch-suggestions="querySearchAsync"
                  placeholder="请输入内容"
                  @select="handleSelect"
                ></el-autocomplete>
          </el-form-item>
          <el-form-item label="等级">
            <el-select v-model="formInline.level" placeholder="等级" clearable="true">
              <el-option label="一级" :value="1" ></el-option>
              <el-option label="二级" :value="2" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作者">
            <el-input v-model="formInline.authorId" placeholder="作者"></el-input>
          </el-form-item>
          <el-form-item label="最早发布时间">
            <div class="block">
            
              <el-date-picker
                v-model="formInline.timeStart"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="最晚发布时间">
            <div class="block">
              
              <el-date-picker
                v-model="formInline.timeEnd"
                type="datetime"
                :fromatter="formatData"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>

        <el-table
            :data="comments"
            stripe="true"
            style="width: auto;">
        
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column label="序号" width="50" type="index" >
            </el-table-column>
            <el-table-column label="作者" width="200">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="top">
                  <p>ID: {{scope.row.id}}</p>
                  <p>作者ID: {{scope.row.author.id}}</p>
                  <p>文章ID: {{scope.row.articleId}}</p>
                  <p>父评论ID: {{scope.row.parentId}}</p>
                  <!-- <p>回复用户ID: {{scope.row.toUser.id}}</p> -->
                  <p>内容: {{scope.row.content}}</p>
                  <div slot="reference" class="name-wrapper">
                     <a  class="me-article-title">{{scope.row.author.nickname}}</a>
                  </div>
                </el-popover>
                
              </template>
            </el-table-column>
             <el-table-column label="状态" width="100">
              <template slot-scope="scope">
                <el-tag type="primary" size="mini" v-if="scope.row.level == 1">一级</el-tag>
                <el-tag type="warning" size="mini" v-else>二级</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="时间" width="200">
              <template slot-scope="scope">
                {{scope.row.createDate}}
              </template>
            </el-table-column>
            </el-table-column>
             <el-table-column label="状态" width="100">
              <template slot-scope="scope">
                <el-tag type="warning" size="mini" v-if="scope.row.status == 0">封禁</el-tag>
                <el-tag type="success" size="mini" v-else>正常</el-tag>
              </template>
            </el-table-column>
           
            <el-table-column label="操作" >
              <template slot-scope="scope">
                 <el-button
                    size="mini"
                    type="warning"
                    @click="closeComment(scope.row)"
                    v-if="scope.row.status == 1"
                    >
                    封禁</el-button>

                  <el-button
                    size="mini"
                    type="success"
                    @click="openComment(scope.row)"
                    v-else
                    >
                    解封
                    </el-button>

                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>

            </el-table-column>
        </el-table>

    

        <el-pagination
          small="false"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="innerPage.pageNumber"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="innerPage.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="innerPage.totalCounts">
        </el-pagination>
    </div>
</template>

<script>
  import {getComments} from '@/api/comment'
  import {changeCommentStatus} from '@/api/comment'
  import {getToken} from '@/request/token'
  import {deleteComment} from '@/api/comment'

  import {searchArticle} from '@/api/article'

  export default {
    data() {
      return {
        innerPage: {
          pageSize: 10,
          pageNumber: 1,
          totalCounts: 0,
          
        },
        comments: [],
        formInline:{},
        timeStart: '',
        timeEnd: '',
        
      }
    },
    props: {
      offset: {
        type: Number,
        default: 100
      },
      page: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    watch: {
      
      'page': {
        handler() {
          this.noData = false
          this.articles = []
          this.innerPage = this.page
          this.getCommentForAdmin()
        },
        deep: true
      }
    },
    computed: {
      user() {
        let login = this.$store.state.account.length != 0
        let admin = this.$store.state.admin
        return {
          login, admin
        }
      }
    },
    created () {

      let token = getToken()
      if(token == null || this.user.login == false || this.user.admin < 1){
        //跳转到首页
        this.$router.push({path: '/'})
      }else{
        this.getCommentForAdmin();
      }
    },
    methods: {
      load() {
        this.getCommentForAdmin()
      },
      view(id) {
        this.$router.push({path: `/view/${id}`})
      },
      getCommentForAdmin() {
        let that = this
        that.loading = true
        let token = getToken()
        getComments(token, that.innerPage, that.formInline).then(data => {

          let newComments = data.data.commentList
          that.innerPage.pageSize = data.data.pageData.size
          that.innerPage.pageNumber = data.data.pageData.current
          that.innerPage.totalCounts = data.data.pageData.total
          that.comments = null
          if (newComments && newComments.length > 0) {
            that.comments = newComments
          } else {
            that.noData = true
          }

        }).catch(error => {
          if (error !== 'error') {
            that.$message({type: 'error', message: '评论加载失败!', showClose: true})
          }
        }).finally(() => {
          that.loading = false
        })

      },
      handleSizeChange(val) {
        let that = this
        that.innerPage.pageSize = val
        this.getCommentForAdmin()
      },
      handleCurrentChange(val) {
        let that = this
        that.innerPage.pageNumber = val
        this.getCommentForAdmin()
      },
      openComment(row){
        let that = this
        let token = getToken()
        changeCommentStatus(token, row.id, 1).then(data => {
         
          that.$message({
              message: '解封成功',
              type: 'success'
            });
          }).catch(error => {
              if (error !== 'error') {
                that.$message({type: 'error', message: '评论解封失败!', showClose: true})
              }
          }).finally(() => {
              that.getCommentForAdmin();
          })
      },
      closeComment(row){
        let that = this
        let token = getToken()
        changeCommentStatus(token, row.id, 0).then(data => {
          that.$message({
              message: '封禁成功',
              type: 'success'
            });
          }).catch(error => {
              if (error !== 'error') {
                that.$message({type: 'error', message: '评论封禁失败!', showClose: true})
              }
          }).finally(() => {
              that.getCommentForAdmin();
          })
      },
      handleDelete(row) {
        let that = this
        let token = getToken()
        this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteComment(token, row.id).then(data => {
            that.$message({
              message: '删除成功',
              type: 'success'
            });
          }).catch(error => {
              if (error !== 'error') {
                that.$message({type: 'error', message: '评论删除失败!', showClose: true})
              }
          }).finally(() => {
              that.getCommentForAdmin();
          })
        }).catch(() => {
          that.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      querySearchAsync(queryString, cb){
        searchArticle(this.formInline.articleId).then((res)=>{
          if(res.success){
            var results = [];
            for(const item of res.data){
              results.push({
                id:item.id,
                value:item.title
              });
            }
            cb(results)
          }
        })

      },
      handleSelect(item){
        //this.$router.push({path: '/view/'+item.id})
        this.formInline.articleId = item.id
      },
      
      onSubmit() {
        this.getCommentForAdmin();
      },
      
      
    }
  }
</script>

<style scoped>

    

</style>